package win.demonlegion.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import win.demonlegion.mapper.SysRoleAuthorityMapper;
import win.demonlegion.mapper.SysRoleMapper;
import win.demonlegion.mapper.SysUserMapper;
import win.demonlegion.module.SysAuthorityBean;
import win.demonlegion.module.SysUserBean;
import win.demonlegion.module.UserSaltBean;
import win.demonlegion.service.inte.SysAuthorityService;
import win.demonlegion.service.inte.SysRoleAuthorityService;
import win.demonlegion.service.inte.SysUserService;
import win.demonlegion.service.inte.UserSaltService;
import win.demonlegion.util.GuidUtil;
import win.demonlegion.util.MD5Util;
import win.demonlegion.util.SHAUtil;
import win.demonlegion.util.TimestampUtil;

import java.util.List;

/**
 * 后台运营账号管理
 * Created by john on 2017/6/29.
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService, UserDetailsService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private UserSaltService userSaltService;
    @Autowired
    private SysRoleAuthorityService sysRoleAuthorityService;

    @Override
    public int count() {
        return 0;
    }

    @Override
    public List<SysUserBean> findAll() { return sysUserMapper.findAll(); }

    @Override
    public SysUserBean selectById(String id) {
        SysUserBean sysUserBean = sysUserMapper.selectById(id);
        List<SysAuthorityBean> authorities =
                sysRoleAuthorityService.getUserAuthority(sysUserBean.getId());
        sysUserBean.setAuthorityList(authorities);
        return sysUserBean;
    }

    @Override
    public int add(SysUserBean sysUserBean) {
        sysUserBean.setId(GuidUtil.getUuid());
        sysUserBean.setPassword(SHAUtil.SHA512(sysUserBean.getPassword()));
        sysUserBean.setCreateTime(TimestampUtil.getDatetime());
        sysUserBean.setModifyTime(sysUserBean.getCreateTime());
        return sysUserMapper.add(sysUserBean);
    }

    @Override
    public int update(SysUserBean sysUserBean) {
        sysUserBean.setModifyTime(TimestampUtil.getDatetime());
        if(sysUserBean.getPassword() != null && !sysUserBean.getPassword().equals("")) {
            sysUserBean.setPassword(MD5Util.MD5(sysUserBean.getPassword()));
        }
        return sysUserMapper.update(sysUserBean);
    }

    @Override
    public int deleteById(String id) { return sysUserMapper.deleteById(id); }

    @Override
    public SysUserBean login(String username, String password) {
        SysUserBean sysUserBean = sysUserMapper.loadUserByUsername(username);
        String salt = userSaltService.selectByUserId(sysUserBean.getId());
        password = SHAUtil.SHA512(password + salt);
        if(StringUtils.equals(sysUserBean.getPassword(), password)) {
            List<SysAuthorityBean> authorities =
                    sysRoleAuthorityService.getUserAuthority(sysUserBean.getId());
            sysUserBean.setAuthorityList(authorities);
            return  sysUserBean;
        }
        else  return null;
    }

    @Override
    public int disableSysUser(String id) { return sysUserMapper.disableSysUser(id); }

    @Override
    public int enableSysUser(String id) { return sysUserMapper.enableSysUser(id); }

    @Override
    public int changePassword(String id, String oldPassword, String newPassword) {
        oldPassword = MD5Util.MD5(oldPassword);
        newPassword = MD5Util.MD5(newPassword);
        return sysUserMapper.changePassword(id, oldPassword, newPassword);
    }

    @Override
    public int resetPassword(String id, String password) {
        password = MD5Util.MD5(password);
        return sysUserMapper.resetPassword(id, password);
    }

    @Override
    public List<String> getUserRole(String userId) {
        return sysRoleMapper.getUserRole(userId);
    }

    @Override
    public SysUserBean loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserBean sysUserBean = sysUserMapper.loadUserByUsername(username);

        if (sysUserBean == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
        } else {
//            sysUserBean.setRoles(getUserRole(sysUserBean.getId()));
            return sysUserBean;
        }
    }
}
